<!--
 * @Date: 2025-08-01 14:09:00
 * @LastEditors: Csir 2215995096@qq.com
 * @LastEditTime: 2025-08-01 16:45:01
 * @FilePath: \manage_ui\src\views\factoryInfo\index.vue
-->
<template>
  <el-card class="card_outsides">
    <template #header>
      <div class="card-header">
        <span>工厂信息</span>
      </div>
    </template>
    <div class="sync-time-btn">
      <el-button type="success" class="sync-btn" @click="syncData('current')">同步数据</el-button>
      <span class="sync-time"> 最近同步时间：{{ lastSyncTime || '--' }} </span>
    </div>
    <!-- <ContentWrap> -->
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
      style="margin: 20px 0 5px"
    >
      <el-form-item label="工厂编号" prop="factoryCode">
        <el-input
          v-model="queryParams.factoryCode"
          placeholder="请输入工厂编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="工厂名称" prop="factoryName">
        <el-input
          v-model="queryParams.factoryName"
          placeholder="请输入工厂名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <!--        <el-button-->
        <!--          type="primary"-->
        <!--          plain-->
        <!--          @click="openForm('create')"-->
        <!--          v-hasPermi="['factoryinfo::create']"-->
        <!--        >-->
        <!--          <Icon icon="ep:plus" class="mr-5px" /> 新增-->
        <!--        </el-button>-->
        <!--        <el-button-->
        <!--          type="success"-->
        <!--          plain-->
        <!--          @click="handleExport"-->
        <!--          :loading="exportLoading"-->
        <!--          v-hasPermi="['factoryinfo::export']"-->
        <!--        >-->
        <!--          <Icon icon="ep:download" class="mr-5px" /> 导出-->
        <!--        </el-button>-->
      </el-form-item>
    </el-form>
    <!-- </ContentWrap> -->

    <!-- 列表 -->
    <!-- <ContentWrap> -->
    <el-table
      v-loading="loading"
      :data="list"
      :stripe="true"
      border
      :show-overflow-tooltip="true"
      :header-cell-style="{ background: '#f5f7fa', color: '#606266', textAlign: 'center' }"
      :cell-style="{ textAlign: 'center' }"
    >
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="工厂编号" align="center" prop="factoryCode" />
      <el-table-column label="工厂名称" align="center" prop="factoryName" />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['factoryinfo::update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['factoryinfo::delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- </ContentWrap> -->

    <!-- 表单弹窗：添加/修改 -->
    <Form ref="formRef" @success="getList" />
  </el-card>
</template>

<script setup lang="ts">
import download from '@/utils/download'
import { Api, VO } from '@/api/factoryinfo/'
import Form from './Form.vue'
import { currentTime } from '@/utils/currentTime'

/** 工厂数据表，用于存储工厂的基本信息 列表 */
defineOptions({ name: '' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<VO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  factoryCode: undefined,
  factoryName: undefined
})

const lastSyncTime = ref('2025/06/06 20:00:00')
const asyncTime = () => {
  lastSyncTime.value = currentTime()
}

const syncData = (val) => {}

const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
asyncTime()

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await Api.getPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await Api.delete(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await Api.export(queryParams)
    download.excel(data, '工厂数据表，用于存储工厂的基本信息.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>
.sync-time-btn {
  display: flex;
  margin-bottom: 10px;
  .sync-btn {
    background: #b6e03e;
    color: #fff;
    border: none;
    margin-right: 10px;
  }

  .sync-time {
    /* border: 1px solid #f00; */
    color: #333;
    padding: 2px 8px;
    margin-right: auto;
    margin-left: 5px;
  }
}
</style>
